/*
 * Copyright (c) Huawei Device Co., Ltd. 2023. All rights reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "session.h"

#include <gtest/gtest.h>
#include <memory>

#include "channel.h"
#include "ipc_object_stub.h"
#include "loghelper.h"
#include "reader.h"
#include "se_service.h"
#include "service_manager.h"
#include "access_control_enforcer.h"
#include "secure_element_agent.h"

namespace OHOS {
namespace SE {
namespace TEST {
using namespace testing::ext;
using namespace testing;
using namespace OHOS::SE::OMAPI;
using namespace OHOS::SE;
static const std::string CHANNEL_NUMBER = "A000000151000000";
static const std::string GET_CPLC = "80CA9F7F00";
static bool g_isSeSupported = false;

class AccessRuleEnforcerTest : public testing::Test {
public:
    static void SetUpTestCase() {};
    static void TearDownTestCase() {};
    void SetUp();
    void TearDown();
public:
    std::shared_ptr<AccessControlEnforcer> accessEnforcer_;
    std::shared_ptr<SecureElementAgent> agent_;
    std::weak_ptr<SecureElementAgent> weakAgent_;
    std::shared_ptr<SEService> seService_;
    std::shared_ptr<Reader> reader_;
    std::shared_ptr<Session> session_;
    std::shared_ptr<Channel> channel_;
};

void AccessRuleEnforcerTest::SetUp()
{
    InfoLog("AccessRuleEnforcerTest::SetUp begin!");
    agent_ = std::make_shared<SecureElementAgent>("eSE");
    weakAgent_ = agent_;
    accessEnforcer_ = std::make_shared<AccessControlEnforcer>(agent_);
    seService_ = std::make_shared<SEService>();
    bool initStatus = false;
    seService_->InitSEService(initStatus, nullptr);
    sleep(1);
    EXPECT_TRUE(initStatus);
    vector<std::shared_ptr<Reader>> readerLists;
    seService_->GetReaders(readerLists);
    g_isSeSupported = readerLists.empty();
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    for (auto reader : readerLists) {
        std::string readerName {};
        reader->GetName(readerName);
        if (readerName.compare("eSE") == 0) {
            reader_ = reader;
            break;
        }
    }
    EXPECT_TRUE(reader_);
    if (reader_ != nullptr) {
        reader_->OpenSession(session_);
    }
    EXPECT_TRUE(session_);
    if (session_ != nullptr) {
        std::string response = "";
        session_->OpenLogicalChannel(CHANNEL_NUMBER, 0, channel_, response);
    }

    EXPECT_TRUE(channel_);
    InfoLog("AccessRuleEnforcerTest::SetUp done!");
}

void AccessRuleEnforcerTest::TearDown()
{
    InfoLog("AccessRuleEnforcerTest::TearDown begin!");
    if (g_isSeSupported) {
        ErrorLog("se not support!");
        return;
    }
    if (channel_) {
        channel_->Close();
        channel_ = nullptr;
    }
    if (session_) {
        session_->Close();
        session_ = nullptr;
    }
    if (reader_) {
        reader_->CloseSessions();
        reader_ = nullptr;
    }
    if (seService_) {
        seService_->Shutdown();
        seService_ = nullptr;
    }
    InfoLog("AccessRuleEnforcerTest::TearDown done!");
}

/**
 * @tc.name: IsChannelAccessAllowed_Test001
 * @tc.desc: Test Session GetReader.
 * @tc.type: FUNC
 */
HWTEST_F(AccessRuleEnforcerTest, IsChannelAccessAllowed_Test001, TestSize.Level1)
{
    if (accessEnforcer_ != nullptr) {
        const uint32_t uid = 0;
        bool statusCode = accessEnforcer_->IsChannelAccessAllowed(uid);
        EXPECT_FALSE(!statusCode);
    }
}

/**
 * @tc.name: GetATR_Test001
 * @tc.desc: Test Session GetATR.
 * @tc.type: FUNC
 */
HWTEST_F(AccessRuleEnforcerTest, IsAllowedByAidAndAppId_Test001, TestSize.Level1)
{
    std::weak_ptr<SecureElementAgent> agent;
    std::vector<uint8_t> aid;
    uint32_t uid = 0;
    std::shared_ptr<AccessControlEnforcer> accessControlEnforcer =
        std::make_shared<AccessControlEnforcer>(true, true, agent);
    bool statusCode = accessControlEnforcer->IsAllowedByAidAndAppId(aid, uid);
    EXPECT_FALSE(statusCode);
}

/**
 * @tc.name: IsClosed_Test001
 * @tc.desc: Test Session IsClosed.
 * @tc.type: FUNC
 */
HWTEST_F(AccessRuleEnforcerTest, IsAllowedByFingerPrint_Test001, TestSize.Level1)
{
    if (accessEnforcer_ != nullptr) {
        uint32_t uid = 0;
        bool statusCode = accessEnforcer_->IsAllowedByFingerPrint(uid);
        EXPECT_FALSE(statusCode);
    }
}

} // TEST
} // SE
} // OHOS